package com.liujc.virtualapkdemo;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.didi.virtualapk.PluginManager;
import com.didi.virtualapk.internal.LoadedPlugin;

import java.io.File;

import static android.os.Environment.getExternalStorageDirectory;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    public static final String IMG1_URL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510290258030&di=e851b9a55dca58e848010c91ccbabadc&imgtype=0&src=http%3A%2F%2Fg.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F838ba61ea8d3fd1f571a04d03a4e251f94ca5fcc.jpg";
    public static final String IMG2_URL = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1510290264866&di=b9616f5a0c5b7d73d0cceb9a32cf53d5&imgtype=0&src=http%3A%2F%2Fimg.mp.itc.cn%2Fupload%2F20161102%2F93a9cfc021ed44b28af7a143be6ba017_th.jpeg";
    public static final String PLUGIN_NAME = "pluginA.apk";
    public static final String PLUGIN_PKG_NAME = "com.liujc.plugin";
    public static final String PLUGIN_CLASS_NAME = "com.liujc.plugin.PluginMainActivity";
    private TextView showPluginInfo;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        loadPlugin(this);

    }

    private void initView() {
        ImageView homeIconIv1 = (ImageView) findViewById(R.id.iv_home_icon1);
        ImageView homeIconIv2 = (ImageView) findViewById(R.id.iv_home_icon2);
        Button imageBrowserBtn = (Button) findViewById(R.id.btn_image_browser);
        imageBrowserBtn.setOnClickListener(this);
        Glide.with(this).load(IMG1_URL).into(homeIconIv1);
        Glide.with(this).load(IMG2_URL).into(homeIconIv2);
        showPluginInfo = (TextView) findViewById(R.id.show_plugin_info);
    }

    @Override
    public void onClick(View v) {
        if (PluginManager.getInstance(this).getLoadedPlugin(PLUGIN_PKG_NAME) == null) {
            Toast.makeText(getApplicationContext(),
                    "插件未加载,请尝试重启APP", Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent();
        intent.setClassName(PLUGIN_PKG_NAME, PLUGIN_CLASS_NAME);
        startActivity(intent);
    }

    private void loadPlugin(Context base) {
        PluginManager pluginManager = PluginManager.getInstance(base);
        File apk = new File(getExternalStorageDirectory(),PLUGIN_NAME);
        if (apk.exists()) {
            try {
                pluginManager.loadPlugin(apk);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(getApplicationContext(),
                    "SDcard根目录未检测到plugin.apk插件", Toast.LENGTH_SHORT).show();
        }
        LoadedPlugin loadedPlugin = pluginManager.getLoadedPlugin(PLUGIN_PKG_NAME);
        if (loadedPlugin != null){
            String versionInfo = "Host版本信息："+getAppVersionName(MainActivity.this)+"\n"+
                    "插件版本信息："+getAppVersionName(loadedPlugin.getPluginContext());

            showPluginInfo.setText(versionInfo);
        }
    }

    /**
     * 返回当前程序版本名
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        int versioncode = 0;
        try {
            // ---get the package info---
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            versioncode = pi.versionCode;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            Log.e("VersionInfo", "Exception", e);
        }
        return "V"+versionName+" | "+versioncode;
    }
}
